import WeCropper from '../../utils/we-cropper/we-cropper.js'
const app = getApp()

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({
  data: {
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    },
    id: '',
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    var that = this;
    this.wecropper.getCropperImage((avatar) => {
      if (avatar) {
        //  获取到裁剪后的图片
        console.log(avatar)
        app.uploadToQiNiu({
          filePath: avatar,
          success: function (r) {
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; // 上一级页面
            prevPage.toggle();
            prevPage.setData({
              'userInfo.avatarUrl': r.imageURL,
              // ["detail.orgUrl"]: r.imageURL
            })
            wx.navigateBack({})
            // app.req({
            //   url: "wechat/user/updateUserInformation",
            //   method: "post",
            //   data:{
            //     avatarUrl: r.imageURL
            //   },
            //   success: function (res) {
            //     console.log('修改头像:' + JSON.stringify(res))
            //     if (200 == res.code) {
            //       wx.showToast({
            //         title: res.msg,
            //         icon: 'none',
            //         duration: 2000
            //       })
            //       wx.navigateBack({})
            //     } else {
            //       console.log("ssssssss---", res.msg)
            //       wx.showToast({
            //         title: res.msg,
            //         icon: 'none',
            //         duration: 2000
            //       })
            //     }
            //   }
            // });
            // wx.navigateBack({})
            // that.toggle("detail.orgUrl", r.imageURL);
          },
          error: function (r) {
            console.log(r)
          }
        });
        // wx.redirectTo({
        //   url: `../personalHome/personalHome?avatar=${avatar}`
        // })
      } else {
        console.log('获取图片失败，请稍后重试')
      }
    })
  },

  uploadTap() {
    const self = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值

        self.wecropper.pushOrign(src)
      }
    })
  },
  onLoad(option) {
    const {
      cropperOpt
    } = this.data;

    if (option.src) {
      cropperOpt.src = option.src
      new WeCropper(cropperOpt)
        .on('ready', (ctx) => {
          console.log(`wecropper is ready for work!`)
        })
        .on('beforeImageLoad', (ctx) => {
          console.log(`before picture loaded, i can do something`)
          console.log(`current canvas context:`, ctx)
          wx.showToast({
            title: '上传中',
            icon: 'loading',
            duration: 20000
          })
        })
        .on('imageLoad', (ctx) => {
          console.log(`picture loaded`)
          console.log(`current canvas context:`, ctx)
          wx.hideToast()
        })
        .on('beforeDraw', (ctx, instance) => {
          console.log(`before canvas draw,i can do something`)
          console.log(`current canvas context:`, ctx)
        })
        .updateCanvas()
    }
  }
})